<?php

namespace app\admin\controller\business;

use app\common\controller\Backend;

/**
 * 
 */
class Highseainfo extends Backend
{
    // 快速查询，搜索指定的哪些字段
    // protected  $searchFields = 'visit.id,nickname,source.name';
    /**
     * 当前控制器下的一个模型属性
     */
    protected $model = null;

    // 初始化
    public function _initialize()
    {
        parent::_initialize();

        // 全局用户模型
        $this->BusinessModel = model('Business.Business');

        // 回访表
        $this->VisitModel = model('Business.Visit');

        $this->AdminModel = model('Admin');

        $this->ReceiveModel = model('Business.Receive');

        $this->Adminlist = $this->AdminModel->select();

        $Admindata = [];

        foreach ($this->Adminlist as $k => &$v) {

            $Admindata[$v['id']] = $v['username'];
        }
        $this->view->assign('Admindata', $Admindata);
    }

    public function index($ids = null)
    {
        $ids = $ids ?  $ids : 0;

        // 客户详情
        $row = $this->BusinessModel->with('source')->where('adminid', 'NULL')->find($ids);
     
        $this->assign(['row' => $row]);

        return $this->view->fetch();
    }


    // 回访列表
    public function visit($ids = null)
    {
       
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            // 此处将筛选请求中有争议的字段"id"指定为tableA中的id
            $this->changeWhereName("id", "visit.id");
            $this->changeWhereName("createtime", "visit.id");

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->VisitModel
                ->with(['admin','business'])
                ->where($where)
                ->where('busid', $ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->VisitModel
                ->with(['admin','business'])
                ->where($where)
                ->where('busid', $ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }

     // 申请记录
    public function receive($ids = null)
    {
 
         //设置过滤方法
         $this->request->filter(['strip_tags']);
 
         if ($this->request->isAjax()) {
             if ($this->request->request('keyField')) {
                 return $this->selectpage();
             }
 
             // 此处将筛选请求中有争议的字段"id"指定为tableA中的id
             $this->changeWhereName("id", "receive.id");
 
             list($where, $sort, $order, $offset, $limit) = $this->buildparams();
 
             $total = $this->ReceiveModel
                 ->with(['admin','business'])
                 ->where($where)
                 ->where('busid', $ids)
                 ->order($sort, $order)
                 ->limit($offset, $limit)
                 ->count();  //查询总数
 
             $list = $this->ReceiveModel
                 ->with(['admin','business'])
                 ->where($where)
                 ->where('busid', $ids)
                 ->order($sort, $order)
                 ->limit($offset, $limit)
                 ->select();  //查询数据
 
             $result = array("total" => $total, "rows" => $list);
 
             return json($result);
         }
 
         return $this->view->fetch();
     }

}
